package cn.itcast.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.User;
import cn.itcast.service.RoleService;
import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;
    /**
     * 异步查询用户名
     * @return
     */
    @GetMapping("getUsername")
    public Result queryUserName(){
        //获取ss存储的用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDetails userDetails = (UserDetails) authentication.getPrincipal();
        //获取用户名
        String username = userDetails.getUsername();
       String user= userService.findByUserName(username);
       if (user!=null){
           return new Result(true,MessageConstant.QUERY_USER_SUCCESS,user);
       }
       return new Result(false,MessageConstant.QUERY_USER_FAIL);
    }
}
